<?php
namespace Admin\Controller;
use Think\Page;
class AdminController extends CommonController
{
    public function add()
    {
        if (IS_GET) {
            //获取所有的角色
            $role = D('Role')->select();
            $this->assign('role', $role);
            $this->display();
        } else {
            //$model = D('Admin');
            $data = I('post.');

            $map['username'] = $data['username'];
            $map['password'] = md5($data['password']);
            $admin_id = M('admin')->add($map);
            if ($admin_id) {
                $maps['role_id'] = $data['role_id'];
                $maps['admin_id'] = $admin_id;
                $rel = M('admin_role')->add($maps);

            }
            if (!$rel) {
                $this->error();
            }

            $this->success('写入数据成功', U('index'));
        }
    }

    //获取数据显示
    public function index()
    {
        $model = D('Admin');
        $data = $model->listData();
        //dump($data);die;
        $this->assign('data', $data);
        $this->display();
    }

    //删除管理员
    public function dels()
    {
        $admin_id = intval(I('get.admin_id'));

        if ($admin_id < 1) {
            $this->error('参数错误');
        }
        $res = D('Admin')->remove($admin_id);
        if ($res === false) {
            $this->error("删除失败");
        }
        $this->success('删除成功');
    }

    //编辑
    public function edit()
    {
        $model = D('Admin');
        if (IS_GET) {
            $admin_id = intval(I("get.admin_id"));
            //获取用户名信息，及角色id
            $info = $model->findOne($admin_id);

            //获取所有的角色
            $role = D('Role')->select();

            $this->assign("role", $role);
            $this->assign("info", $info);
            $this->display();
        } else {
            //$data = $model->create();
            $data =I('post.');
            $map['username']=$data['cname'];
            $map['password']=md5($data['password']);
            $admin = M('admin')->where(array('id'=>$data['id']))->save($map);

            $maps['role_id'] = $data['role_id'];
            $role = M('admin_role')->where(array('admin_id'=>$data['id']))->save($maps);

            $this->success("修改成功", U('index'));
        }
    }

    /*
   *
   * 管理员登录记录列表
   * */
    public function admin_login(){

        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");
        $obj=M("admin_login");
        $num=10;
        if($type_id==5){
            $arr='';
            $admin_id=M("user_admin")->field('admin_id')->where("name like '%{$key_world}%'")->select();
            foreach($admin_id as $k=>$v){
                $arr.=$v['admin_id'].',';
            }
            $arr=trim($arr,',');
            if($arr==''){//排除IN 询条件为空导致的 sql 语法错误
                $this->redirect("admin_login",'',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and admin_id in($arr)")->count();
                $Page=new Page($count,$num);
                $page=$Page->show();
                $data=$obj->where("del=0 and admin_id in($arr)")->limit("$Page->firstRow,$Page->listRows")->order("login_time desc")->select();
            }
        }else{
            $count=$obj->where("del=0")->count();
            $Page=new Page($count,$num);
            $page=$Page->show();
            $data=$obj->where("del=0")->limit("$Page->firstRow,$Page->listRows")->order("login_time desc")->select();
        }
        //显示管理员
        foreach($data as $infok=>$infov){
            $goods_name=M("user_admin")->field("name")->where("admin_id='".$infov['admin_id']."'")->find();
            $data[$infok]['admin_ids']=$goods_name['name'];
        }
        $type=array(
            '5'=>'管理员'
        );
        $this->assign('type',$type);
        $this->assign("page",$page);
        $this->assign("data",$data);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
      *
      * 管理员登录记录列表删除
      * */
    public function admin_logindel(){
        $id=I('id');
        $data['del']=1;
        $re=M("admin_login")->where("record_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('admin_login','',3,'删除成功！');
        }else{
            $this->redirect('admin_login','',3,'删除失败！');
        }
    }


    /*
  *
  * 管理员操作记录列表
  * */
    public function admin_operation(){

        $type_id=I("post.type_id");
        $key_world=I("post.goods_name");
        $obj=M("admin_operation");
        $num=10;
        if($type_id==5){
            $arr='';
            $admin_id=M("user_admin")->field('admin_id')->where("name like '%{$key_world}%'")->select();
            foreach($admin_id as $k=>$v){
                $arr.=$v['admin_id'].',';
            }
            $arr=trim($arr,',');
            if($arr==''){//排除IN 询条件为空导致的 sql 语法错误
                $this->redirect("admin_operation",'',3,'没有匹配的记录！');
            }else{
                $count=$obj->where("del=0 and admin_id in($arr)")->count();
                $Page=new Page($count,$num);
                $page=$Page->show();
                $data=$obj->where("del=0 and admin_id in($arr)")->limit("$Page->firstRow,$Page->listRows")->order('operation_time desc')->select();
            }
        }else{
            $count=$obj->where("del=0")->count();
            $Page=new Page($count,$num);
            $page=$Page->show();
            $data=$obj->where("del=0")->limit("$Page->firstRow,$Page->listRows")->order('operation_time desc')->select();
        }
        //显示管理员
        foreach($data as $infok=>$infov){
            $goods_name=M("user_admin")->field("name")->where("admin_id='".$infov['admin_id']."'")->find();
            $data[$infok]['admin_ids']=$goods_name['name'];
        }
        $type=array(
            '5'=>'管理员'
        );
        $this->assign('type',$type);
        $this->assign("page",$page);
        $this->assign("data",$data);
        //传递搜索条件
        $this->assign('type_id',$type_id);
        $this->assign('key_world',$key_world);
        $this->display();
    }

    /*
      *
      * 管理员操作记录列表删除
      * */
    public function admin_operationdel(){
        $id=I('id');
        $data['del']=1;
        $re=M("admin_operation")->where("record_id='".$id."'")->save($data);
        if($re!=false){
            $this->redirect('admin_operation','',3,'删除成功！');
        }else{
            $this->redirect('admin_operation','',3,'删除失败！');
        }
    }
}
